--[[ 婆娑罗,从雷鸟之翼改过来,但却是小端,不能用read_u16!
  
  改装到MyMame模块和MySTG模块,Y坐标需要反转
  
  By setycyas @2025-10-18
]]

--防止重复运行
if (_G.game) then
  print('script already loaded!')
  return
end

local myMame = require('MyMame')
local myStg = require('MySTG')

-- 这里设定基本参数
local GAME = {
-- 脚本名,防重加载用
  NAME = "vasara_autoboot",
  -- 分辨率
  X_RESOLUTION = 336,
  Y_RESOLUTION = 240,
  -- 横坐标纵坐标内存地址
  X_ADDR_HIGH = 0xffb4+1,
  X_ADDR_LOW = 0xffb4,
  Y_ADDR_HIGH = 0xffb8+1,
  Y_ADDR_LOW = 0xffb8,
  -- 能量地址,最大106
  POWER_ADDR = 0x2EE6,
  -- 能量分界线定义,高于该数值线颜色改变提示
  POWER_LINE = 76,
  -- 坐标修正像素
  X_OFFSET = 7,
  Y_OFFSET = 3,
  -- 自己中心方块的半长半高,单位为MAME的屏幕比例
  --BOX_HALF_WIDTH = 0.005,
  --BOX_HALF_HEIGHT = 0.005,
  BOX_WIDTH = 5,
  BOX_HEIGHT = 5,
  -- 中心方块的边框颜色和中心颜色
  BOX_BORDER_COLOR = 0x880000ff,
  BOX_COLOR = 0x880000cc,
  -- 十字线宽度和颜色,能量高于一定数值时线颜色改变作出提示
  LINE_WIDTH = 1.1,
  LINE_COLOR = 0x8800ffff,
  LINE_POWER_COLOR = 0x88ff0000,
  -- 中途需要变更的测试参数,准备了10个
  TEST_PARAMS = {64.0, 64.0, 208.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  -- mame api用的对象
  mem = manager.machine.devices[':maincpu'].spaces['program'],
  container = manager.machine.screens[":screen"].container,
  ui_container = manager.machine.render.ui_container,
}
-- 更新一下模块的参数
myMame.gameName = GAME.NAME
myMame.screenIndex = ':screen'
myStg.screenIndex = ':screen'
myStg.X_RESOLUTION = GAME.X_RESOLUTION
myStg.Y_RESOLUTION = GAME.Y_RESOLUTION

-- 坐标获取,返回整数,Y需要反转
function GAME.getX_u8()
  local result = -1
  local xlow = myMame.mem:read_u8(GAME.X_ADDR_LOW)
  local xhigh = myMame.mem:read_u8(GAME.X_ADDR_HIGH)
  -- 高位过大,位置不对
  if (xhigh > 1) then
    return -1
  end
  if (xhigh == 1) then
    result = 0x100+xlow
  else
    result = xlow
  end
  return result+GAME.X_OFFSET
end
function GAME.getY_u8()
  local result = -1
  local ylow = myMame.mem:read_u8(GAME.Y_ADDR_LOW)
  local yhigh = myMame.mem:read_u8(GAME.Y_ADDR_HIGH)
  -- 高位过大,位置不对
  if (yhigh > 1) then
    return -1
  end
  if (yhigh == 1) then
    result = 0x100+ylow
  else
    result = ylow
  end
  return result+GAME.Y_OFFSET
end

-- 数据显示,指定位置和颜色,然后是可变的多个显示数据
function GAME.showInfo(position, color, ...)
  local count = select("#", ...)
  
  for i = 1, count do
    local arg = select(i, ...)
    GAME.ui_container:draw_text(position, 1.01-0.05*i, arg, color, 0xFF839496)
  end
  
end

-- 监视数据显示
function GAME.check()
  local _power --内存数据
  _power = GAME.mem:read_u8(GAME.POWER_ADDR)
  -- 显示当前火力等级
  GAME.showInfo(0.82, 0xFF000000, 
    string.format(" POW=%d", _power)
  )
end

-- 显示自机的几个方框,xfloat,yfloat是浮点数坐标
-- 显示中心
function GAME.showCenter(x, y)

  myStg.centerBox(
    x, y,
    GAME.BOX_WIDTH, GAME.BOX_HEIGHT,
    GAME.BOX_BORDER_COLOR, GAME.BOX_COLOR
  )
  
end
  
-- 显示十字线
function GAME.showCrossLine(x, y)

  local _power --内存数据
  _power = GAME.mem:read_u8(GAME.POWER_ADDR)
  if (_power < GAME.POWER_LINE) then
    myStg.xLine(x, GAME.LINE_COLOR, GAME.LINE_WIDTH)
    myStg.yLine(y, GAME.LINE_COLOR, GAME.LINE_WIDTH) 
  else
    myStg.xLine(x, GAME.LINE_POWER_COLOR, GAME.LINE_WIDTH)
    myStg.yLine(y, GAME.LINE_POWER_COLOR, GAME.LINE_WIDTH) 
  end
end

--[[ **************************
  主函数,绑定后每一帧执行一次该函数
]]
function GAME.Main()
  local x, y

  -- 获取整数坐标
  x = GAME.getX_u8()
  y = GAME.getY_u8()

  -- 数据监视
  GAME.check()
  -- 显示角色中心辅助线
  if (x > 0) and (y > 0) then
    -- 十字线
    GAME.showCrossLine(x, y)
    -- 中心
    GAME.showCenter(x, y)
  end
  
end

--[[运行]]

-- 绑定每一帧执行的函数,测试时绑定到debug
--myMame.debugRunPerFrame(GAME.Main, 'vasara')
myMame.runPerFrame(GAME.Main, 'vasara')
-- 导出到全局变量
_G.game = GAME

return GAME
